package com.runssnail.demo.spring.web.action.member;

import com.runssnail.demo.spring.biz.domain.MemberDO;
import com.runssnail.demo.spring.biz.manager.MemberManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Created by zhengwei on 2016/2/4.
 */
@Controller
@RequestMapping("member")
public class MemberAction {

    @Autowired
    private MemberManager memberManager;

    @RequestMapping(value = "list")
    public void list(Model model) {
        List<MemberDO> data = memberManager.selectAll();
        model.addAttribute("data", data);
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public void addMember(@ModelAttribute("member") MemberDO member) {

    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String addMember(@ModelAttribute("member") MemberDO member, Model model) {
        Long id = memberManager.insert(member);
        model.addAttribute("id", id);
        model.addAttribute("message", "增加成功");
        return "success";
    }

    @RequestMapping("delete")
    public String deleteMember(@RequestParam("id") Long id, Model model) {
        memberManager.deleteById(id);
        model.addAttribute("message", "删除成功");
        return "success";
    }

    @RequestMapping(value = "update", method = RequestMethod.GET)
    public void updateMember(@RequestParam("id") Long id, Model model) {
        MemberDO member = memberManager.getById(id);
        model.addAttribute("member", member);
    }

    @RequestMapping("update")
    public String doUpdateMember(@ModelAttribute("member") MemberDO member, Model model) {
        memberManager.update(member);
        model.addAttribute("message", "修改成功");
        return "success";
    }

    @RequestMapping("get")
    public void getMember(@RequestParam("id") Long id, Model model) {
        MemberDO member = memberManager.getById(id);
        model.addAttribute("member", member);
    }


}
